<?php
class Library_Model_TapTinMapper extends Library_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator =null;	
	
	public function searchByMaVanBan($id)
	{
		$db = $this->getDb();
		$selectTaptin = $db->select()
		->from('tbl_taptin')
		->where('MaVanBan = ?',$id);
		$taptin = $db->fetchAll($selectTaptin);
		$taptinArray = array();
	
		foreach ($taptin as $taptin)
		{
			$taptinObj = new Library_Model_TapTin();
			$taptinObj->setId($taptin['MaTapTin']);
			$taptinObj->setTentaptin($taptin['TenTapTin']);
			$taptinArray[] = $taptinObj;
		}
		return $taptinArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_TapTin();
		$obj->setId($data['MaTapTin']);
		$obj->setTentaptin($data['TenTapTin']);
		
		return $obj;
	}
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		
		$mavanban = $db->lastInsertId('tbl_vanban', 'MaVanBan');
		$data = array(
				'MaVanBan' => $mavanban,
				'TenTapTin' =>$obj->getTentaptin(),
				'Duyet'			=>$obj->getDuyet()
				);
		$db->insert('tbl_taptin', $data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data = array(
				'TenTapTin' =>$obj->getTentaptin(),
				'Duyet'			=>$obj->getDuyet()
				);
		$where = $db->quoteInto('MaTapTin = ?', $obj->getId());
		$db->update('tbl_taptin', $data, $where);
	}
	
	public function delete($id){
		$db = $this->getDb();
		$where = $db->quoteInto('MaVanBan = ?', $id );
		$db->delete('tbl_taptin', $where);
	}
	
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
}